package com.youlu.myapplication.scratch;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;


public class ScratchUtils {
    private static Application application;

    public static void setApplication(Application application) {
        ScratchUtils.application = application;
    }

    public static void save(String name, String str2, Object obj) {
        SharedPreferences.Editor edit = getSharedPreferences(name).edit();
        if (obj instanceof String) {
            edit.putString(str2, (String) obj);
        } else if (obj instanceof Integer) {
            edit.putInt(str2, ((Integer) obj).intValue());
        } else if (obj instanceof Boolean) {
            edit.putBoolean(str2, ((Boolean) obj).booleanValue());
        } else if (obj instanceof Float) {
            edit.putFloat(str2, ((Float) obj).floatValue());
        } else if (obj instanceof Long) {
            edit.putLong(str2, ((Long) obj).longValue());
        } else {
            //sp 存json
            edit.putString(str2, GsonUtils.toJson(obj));
        }
        a.a(edit);
    }

    public static SharedPreferences getSharedPreferences(String name) {
        return application.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    public static <T> T get(String str, String str2, T t) {
        SharedPreferences a2 = getSharedPreferences(str);
        if (t instanceof String) {
            return (T) a2.getString(str2, (String) t);
        }
        if (t instanceof Integer) {
            return (T) Integer.valueOf(a2.getInt(str2, ((Integer) t).intValue()));
        }
        if (t instanceof Boolean) {
            return (T) Boolean.valueOf(a2.getBoolean(str2, ((Boolean) t).booleanValue()));
        }
        if (t instanceof Float) {
            return (T) Float.valueOf(a2.getFloat(str2, ((Float) t).floatValue()));
        }
        return t instanceof Long ? (T) Long.valueOf(a2.getLong(str2, ((Long) t).longValue())) : t;
    }

    public static <T> T getScratchData(String name, String key, Type type) {
        String string = getSharedPreferences(name).getString(key, "");
        if (TextUtils.isEmpty(string)) {
            return null;
        }
        return GsonUtils.fromJson(string, type);
    }

    public static void remove(String str, String str2) {
        SharedPreferences.Editor edit = getSharedPreferences(str).edit();
        edit.remove(str2);
        a.a(edit);
    }

    public static void clear(String str) {
        SharedPreferences.Editor edit = getSharedPreferences(str).edit();
        edit.clear();
        a.a(edit);
    }

    public static boolean contains(String str, String str2) {
        return getSharedPreferences(str).contains(str2);
    }

    public static Map<String, ?> c(String str) {
        return getSharedPreferences(str).getAll();
    }

    static class a {
        private static final Method a = a();

        private static Method a() {
            try {
                return SharedPreferences.Editor.class.getMethod("apply", new Class[0]);
            } catch (NoSuchMethodException unused) {
                return null;
            }
        }

        public static void a(SharedPreferences.Editor editor) {
            try {
                if (a != null) {
                    a.invoke(editor, new Object[0]);
                    return;
                }
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException unused) {
            }
            editor.commit();
        }
    }
}